package cn.edu.jnu.x2020101356;

public class BowlingGame {
    int score = 0;

    private int rolls[] = new int[21];
    private int currentRoll = 0;


    public void roll(int pins) {
        if (pins == 10) {
            rolls[currentRoll] = pins;
            rolls[currentRoll + 1] = 0;
            currentRoll += 2;
        } else {
            rolls[currentRoll++] = pins;
        }
    }

    public void rollMany(int n, int pins) {
        for (int i = 0; i < n; i++)
            roll(pins);
    }


    public int getScore() {
        int score = 0;
        int frameIndex = 0;
        for (int frame = 0; frame < 10; frame++) {
            if (isStrike(frameIndex)) {
                if(rolls[frameIndex + 2]==10){
                    score += 10 + rolls[frameIndex + 2] + rolls[frameIndex + 4];
                }else{
                    score += 10 + rolls[frameIndex + 2] + rolls[frameIndex + 3];
                }
                frameIndex += 2;
            } else if (isSpare(frameIndex)) {
                score += 10 + rolls[frameIndex + 2];
                frameIndex += 2;
            } else {
                score += rolls[frameIndex] +
                        rolls[frameIndex + 1];
                frameIndex += 2;
            }
        }
        return score;
    }

    private boolean isStrike(int frameIndex) {
        return rolls[frameIndex] == 10;
    }

    private boolean isSpare(int frameIndex) {
        return rolls[frameIndex] +
                rolls[frameIndex + 1] == 10;
    }
}
